package Cliente.Vista;

import Cliente.Controlador.Exceptions;
import Cliente.Controlador.GameController;
import java.awt.Dimension;
import java.io.InputStream;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/**
 * Adds a new user
 * @author Angel Enrique
 */
public class NewPlayer extends javax.swing.JFrame {

    private static Properties props = new Properties();
    boolean isEditable;
    GameController game = null;
    private static String id = null;
    private static String nick = null;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public static Properties getProps() {
        return props;
    }

    public NewPlayer(GameController game) {
        isEditable = false;
        this.game = game;
        initComponents();
        UploadProps();
        this.setVisible(true);
        jButton_save.setText(Constantes.TITLE_NEW);
    }

    public NewPlayer(GameController game, String nick) {
        isEditable = true;
        this.game = game;
        this.nick = nick;
        initComponents();
        UploadProps();
        this.setVisible(true);
        jButton_save.setText(Constantes.TITLE_EDIT);
        PutDatesInConstructor();
    }  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
//        jDesktopPane1 = new javax.swing.JDesktopPane();
        jDesktopPane2 = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel_title = new javax.swing.JLabel();
        jLabel_nick = new javax.swing.JLabel();
        jLabel_email = new javax.swing.JLabel();
        jLabel_user = new javax.swing.JLabel();
        jLabel_password = new javax.swing.JLabel();
        jTextFieldNick = new javax.swing.JTextField();
        jTextFieldPassword = new javax.swing.JTextField();
        jTextFieldEmail = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jButton_save = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(102, 153, 255));
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 204), 2, true));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        jLabel_title.setText(props.getProperty(Constantes.LABEL_EDIT));
        
        jLabel_nick.setText(Constantes.PLAYER_NICK);

        jLabel_email.setText(Constantes.PLAYER_EMAIL);

        jLabel_user.setText(Constantes.PLAYER_USER);

        jLabel_password.setText(Constantes.PLAYER_PASSWORD);

        jTextFieldNick.setText("");
        jTextFieldNick.setPreferredSize(new Dimension(70,20));
        jTextFieldPassword.setText("");
        jTextFieldPassword.setPreferredSize(new Dimension(70,20));
        jTextFieldEmail.setText("");
        jTextFieldEmail.setPreferredSize(new Dimension(70,20));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "User" }));

        jButton_save.setText("jButton1");
        
        jButton_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(jButton_save))
                    .addComponent(jLabel_title, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel_password)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel_nick, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextFieldNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel_user, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_email))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(5, 5, 5)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jTextFieldPassword, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_title)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_nick))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_password)
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_email)
                    .addComponent(jTextFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_user)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(71, 71, 71)
                .addComponent(jButton_save)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jPanel1.setBounds(10, 10, 370, 270);
        jDesktopPane2.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void jButton_saveActionPerformed(java.awt.event.ActionEvent evt) {                                             
    
        saveDates();
    }                                            
    // Variables declaration - do not modify
    private javax.swing.JButton jButton_save;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JDesktopPane jDesktopPane2;
    private javax.swing.JLabel jLabel_email;
    private javax.swing.JLabel jLabel_nick;
    private javax.swing.JLabel jLabel_password;
    private javax.swing.JLabel jLabel_title;
    private javax.swing.JLabel jLabel_user;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldNick;
    private javax.swing.JTextField jTextFieldPassword;
    private javax.swing.JTextField jTextFieldEmail;
    // End of variables declaration

    /**
     * SaveDates It is a method that includes the 
     * fields that the user has gone to new player.
     */
    public void saveDates() {

        String str = null;
        String userType;

        str = jTextFieldNick.getText() + "#";
        str = str + jTextFieldPassword.getText() + "#";
        str = str + jTextFieldEmail.getText() + "#";        
        userType = (String) jComboBox1.getSelectedItem();
        
        if (userType.equals(Constantes.PLAYER_USER_TYPE_ADMIN)) {
            str = str + "0";
        } else if (userType.equals(Constantes.PLAYER_USER_TYPE_USER)) {
            str = str + "1";
        } else {
            str = str + "-1";
        }
        System.out.println(str);
        
        if (isEditable) {         
                String string = getId();
                int integer = 0;              
            try { 
                integer = game.modifyUser(Integer.parseInt(string) ,str);  
                if(integer == 1 || integer == 0){ 
                    props.getProperty("error11");
                    this.dispose();          
                }else{  
                    JOptionPane.showMessageDialog(rootPane,"User saved");
                }
            } catch (Exceptions ex) {            
                JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));
                this.dispose();
            }       
        } else {
            try {
                game.addUser(str);
                this.dispose();
            } catch (Exceptions ex) {
               JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));
               this.dispose(); 
            }
        }       
    }
    
    /**
     * Method that calls putDates in the constructor.
     */
    private void PutDatesInConstructor(){
        try {
            PutDates(game.getUser(Integer.parseInt(nick)));
        } catch (Exceptions ex) {
           JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));
        }
    }
    
    /**
     * PutDates recoge un string con los campos del usuario los tokenizer con: 
     * StringTokenizer. Despues los coloca en los casilleros.
     * 
     * @param str 
     * Str = nick#password#email#typeuser
     */
    private void PutDates(String str){
        
        String word = null;
        StringTokenizer  token = null;
        token = new StringTokenizer(str,"#");
        int contador = 0;
        int integer = 0;
 
        while(token.hasMoreTokens()){
            
            word = token.nextToken();
            
            if(contador == 0){
                setId(word); 
            }if(contador == 1){
                jTextFieldNick.setText(word);  
             }if(contador == 2){    
            
                jTextFieldPassword.setText(null);
            }if(contador == 3){ 
                 
                jTextFieldEmail.setText(word);
            }if(contador == 4){ 
               
                integer = Integer.parseInt(word);
                
                if(integer == 1){
                    jComboBox1.setSelectedItem(Constantes.PLAYER_USER_TYPE_USER);
                }if(integer == 0){
                    jComboBox1.setSelectedItem(Constantes.PLAYER_USER_TYPE_ADMIN);
                }
            }
            contador++;
        }
        
        
    }

    private void UploadProps() {
        InputStream entrada;

        try {
            entrada = this.getClass().getResourceAsStream("/LANGEN.properties");
            props.load(entrada);
            entrada.close();
            
            // Add the items to the combobox
            String str = Constantes.PLAYER_USER_TYPE_ADMIN;
            jComboBox1.addItem(str);
            str =  Constantes.PLAYER_USER_TYPE_USER;
            jComboBox1.addItem(str);
        } catch (Exception e) {
        }
    }
}

